package com.nwpu.vote.control;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;

import com.nwpu.vote.model.*;
import com.nwpu.vote.util.*;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class VotePersistentImpl implements VotePersistent {
	
	public static VotePersistent instance = new VotePersistentImpl();
	
	private static Logger logger = Logger.getLogger(VotePersistentImpl.class.getName());
	
	private VotePersistentImpl(){}
	
	public void save(Vote vote) throws PersistentException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(vote);
        } finally {
            pm.close();
		}
	}
	
	public void save(VoteUser user) throws PersistentException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(user);
        } finally {
            pm.close();
		}
	}
	
	@Override
	public void save(Tag tag) throws PersistentException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
            pm.makePersistent(tag);
        } finally {
            pm.close();
		}
	}

	@Override
	public Vote getVote(String id) throws PersistentException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Key key = KeyFactory.createKey(Vote.class.getSimpleName(), Long.valueOf(id));
		Vote vote = pm.getObjectById(Vote.class, key);
	
        return vote; 
	}
	
	@Override
	public VoteUser getVoteUser(String id) throws PersistentException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Key key = KeyFactory.createKey(VoteUser.class.getSimpleName(), Long.valueOf(id));
		VoteUser user = pm.getObjectById(VoteUser.class, key);
	
        return user; 
	}
	
	@Override
	public Tag getTag(String id) throws PersistentException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Key key = KeyFactory.createKey(Tag.class.getSimpleName(), Long.valueOf(id));
		Tag tag = pm.getObjectById(Tag.class, key);
	
        return tag; 
	}

}
